// pages/song/song.js
import request from '../../utils/request'
import moment from 'moment';
import pubsub from 'pubsub-js'
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlay: false,
    musicInfo: {},
    totalTime: '00:00',
    currentTime: '00:00',
    songId: '',
    musicUrl: "",
    width: 0
  },
  setMusicStatus(isPlay) {
    this.setData({
      isPlay
    })
    app.globalData.isPlay = isPlay
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options);
    let songId = options.songId;
    // console.log(songId);
    this.setData({
      songId
    })
    if (app.globalData.isPlay && app.globalData.songId == songId) {
      this.setData({
        isPlay: true
      })
    }
    this.getMusicInfo(songId)
    this.bgManager = wx.getBackgroundAudioManager();
    this.bgManager.onPlay(() => {

      app.globalData.songId = this.data.songId
      console.log( app.globalData.songId);
      this.setMusicStatus(true)
    })
    // 监听背景音频暂停事件
    this.bgManager.onPause(() => {
      this.setMusicStatus(false)
    })
    // 监听背景音频停止事件
    this.bgManager.onStop(() => {
      this.setMusicStatus(false)
    })

    this.bgManager.onTimeUpdate(() => {

      let currentTime = this.bgManager.currentTime * 1000
      this.setData({
        currentTime: moment(currentTime).format('mm:ss'),
        width: currentTime * 450 / (this.bgManager.duration * 1000)
      })
    })

    this.bgManager.onEnded(() => {
      this.setData({
        currentTime: '00:00',
        width: 0
      })
      this.pre({target:{id:'next'}})
    })
  },

  async getMusicInfo(ids) {
    let res = await request('/song/detail', {
      ids
    });
    // console.log(res);
    let musicInfo = res.songs[0]
    this.setData({
      musicInfo,
      totalTime: moment(musicInfo.dt).format('mm:ss')
    })
    wx.setNavigationBarTitle({
      title: musicInfo.name
    })
  },
  pre(event){
    pubsub.subscribe('songId',(msg,songId)=>{
      this.setData({
        songId
      })
      this.getMusicInfo(songId)
      this.controlMusic(true,songId)
      pubsub.unsubscribe('songId')
    })
    pubsub.publish('switchType',event.target.id)
    
  },
  tapLine(event) {
    // console.log(event);
    let width = (event.touches[0].clientX - 54) * 2
    let curTime = width*(this.bgManager.duration * 1000)  /450
    this.setData({
      width,
      currentTime:moment(curTime).format('mm:ss')

    })
  },

  controlPlay() {
    this.setData({
      isPlay: !this.data.isPlay
    })
    let {
      isPlay,
      songId,
      musicUrl
    } = this.data;
    this.controlMusic(isPlay, songId, musicUrl)
  },
  async controlMusic(isPlay, id, musicUrl) {
    if (!isPlay) {
      //暂停音乐
      this.bgManager.pause()
      return
    }
    if (!musicUrl) {
      let res = await request('/song/url', {
        id
      })
      // console.log(res);
      musicUrl = res.data[0].url
      this.setData({
        musicUrl
      })
    }

    if (!musicUrl) {
      wx.showToast({
        title: '请充钱',
        icon: "none"
      })
      return
    }


    this.bgManager.src = musicUrl
    this.bgManager.title = this.data.musicInfo.name

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})